﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication1.DTO;
using System.Data.SqlClient;

namespace WindowsFormsApplication1.DAO
{
    class LoaiCongViecDAO
    {
        public static List<LoaiCongViecDTO> GetList()
        {
            SqlConnection ketNoi = AbstractDAO.Connection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketNoi;
            cmd.CommandText = @"SELECT * FROM LOAICONGVIEC";

            // Thực thi câu lệnh 
            List<LoaiCongViecDTO> ds = new List<LoaiCongViecDTO>();
            SqlDataReader boDoc = cmd.ExecuteReader();
            while (boDoc.Read())
            {
                LoaiCongViecDTO lcv = new LoaiCongViecDTO();
                lcv.id = boDoc.GetInt32(0);
                lcv.tenCongViec = boDoc.GetString(1);
                lcv.soLuongGoc = boDoc.GetInt32(2);
                lcv.donViTinh = boDoc.GetString(3);
                lcv.heSoThi = float.Parse(boDoc["HeSoThi"].ToString());
                lcv.heSoKiemTra = float.Parse(boDoc["HeSoKiemTra"].ToString());
                ds.Add(lcv);
            }

            boDoc.Close();
            return ds;
        }

        internal static float GetTietCong(int loaiCongViec, int soluong, int lanthi)
        {
            SqlConnection ketNoi = AbstractDAO.Connection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketNoi;
            if(lanthi==1)
                cmd.CommandText = @"SELECT SoLuongGoc, HeSoKiemTra FROM LOAICONGVIEC WHERE ID = "+loaiCongViec;
            else
                cmd.CommandText = @"SELECT SoLuongGoc, HeSoThi FROM LOAICONGVIEC WHERE ID = " + loaiCongViec;

            int soLuongGoc;
            float tietCong = 0, heSo=0;
            SqlDataReader boDoc = cmd.ExecuteReader();
            if (boDoc.Read())
            {
                soLuongGoc = boDoc.GetInt32(0);
                if(lanthi == 1)
                    heSo = float.Parse(boDoc["HeSoKiemTra"].ToString());
                else
                    heSo = float.Parse(boDoc["HeSoThi"].ToString());

                if (loaiCongViec == Def.CV_TOCHUCPHONGTHI)
                {
                    tietCong = (soluong / soLuongGoc) * heSo;
                    if (soluong % soLuongGoc != 0)
                        tietCong += heSo;
                }
                else
                {
                    tietCong = ((float)soluong / soLuongGoc) * heSo;
                }
            }

            boDoc.Close();
            return tietCong;
        }
    }
}
